package com.openatc.agent.service.impl;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.openatc.agent.model.User;
import com.openatc.agent.service.AscsDao;
import com.openatc.agent.service.FeatureDao;
import com.openatc.agent.service.GenericMsgStrategy;
import com.openatc.agent.service.UserDao;
import com.openatc.agent.utils.TokenUtil;
import com.openatc.comm.data.MessageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.List;


/**
 * @Classname CommServiceDown
 * @Description 抽取service层公用的方法
 * @Date 2022/12/19 15:45
 * @Created by kedacom
 */
@Service
public class CommServiceDown {
    @Autowired
    private AscsDao devDao;
    @Autowired
    private TokenUtil tokenUtil;
    @Autowired
    private UserDao userDao;
    @Autowired
    private FeatureDao featureDao;
    private Gson gson = new GsonBuilder().create();
    /**
     * 根据请求/应答消息，计算出指定参数的数量
     * @param messageData
     * @param dataSourceFieldName
     * @return
     */
    public int computeFeatureCount(MessageData messageData, String dataSourceFieldName) {
        JsonElement data = messageData.getData();
        JsonElement featureElement = data.getAsJsonObject().get(dataSourceFieldName);
        if (featureElement == null) {
            return 0;
        }
        return featureElement.getAsJsonArray().size();
    }

    /**
     * 更新dev表中指定字段的数据
     * @param dataSourceFieldName json结构的数据中的key
     * @param fieldName 要更新dev表中字段的名字
     * @param requestData 请求/应答消息
     * @return
     */
    public int updateDevFeatureCount(String dataSourceFieldName, GenericMsgStrategy.FieldName fieldName, MessageData requestData) {
        int count = computeFeatureCount(requestData, dataSourceFieldName);
        return devDao.updateFeatureCount(fieldName,requestData.getAgentid(),count);
    }

    /**
     * 获取当前线程中请求头中的token，返回用户信息
     * @return
     */
    public List<String> getUserRoles() {
        return this.getUserInfo().getRoleNames();
    }

    /**
     * 获取当前线程的用户信息
     * @return
     */
    public User getUserInfo() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String token = attributes.getRequest().getHeader("Authorization");
        String username = tokenUtil.getUsernameFromToken(token);
        User user = userDao.getUserByUserName(username);
        List<String> roleNames = userDao.getRoleNamesByUsername(username);
        user.setRoleNames(roleNames);
        return user;
    }

    /**
     * 是否是管理员
     * @param roles
     * @return
     */
    public boolean isAdminRole(List<String> roles) {
        if (CollectionUtils.isEmpty(roles)) {
            return false;
        }
        return roles.contains("admin") || roles.contains("superadmin");
    }

    /**
     * 是否是管理员
     * @return
     */
    public boolean isAdminRole() {
        List<String> roles = getUserRoles();
        if (CollectionUtils.isEmpty(roles)) {
            return false;
        }
        return roles.contains("admin") || roles.contains("superadmin");
    }

    /**
     * 从数据库中获取所有的特征参数
     * @param agentid
     * @return
     */
    public MessageData getFeatureAllFromDB(String agentid) {
        String data = featureDao.selectFeatureAll(agentid);
        MessageData messageData = gson.fromJson(data, MessageData.class);
        return messageData;
    }
}
